<?php

/**
 * Project: CrazyPoems
 * Author: Alexandru
 * Date: 21/10/2008
 * File: new-poem.php
 */

// Load default settings.
require('inc/config.php');
require('inc/functions.misc.php');
require('inc/functions.index.php');
require('inc/functions.poem.php');

// Get poem id.
$poemId = $_GET['id'];

// Check if user is logged in.
if (!isset($cookie->username)) { header("Location: ".BASE_URL."login.php?next=".BASE_URL."edit-poem.php?id=$poemId"); }

$smarty->assign('pageTitle', l('Editar poesía'));
$smarty->assign('breadcrumbs', '<a href="'.BASE_URL.'">'.l('Inicio').'</a> &raquo; '.l('Editar poesía'));

// Define variables.
$error_msg = "";
$title = isset($_POST['title']) && strlen($_POST['title']) > 0 ? $_POST['title'] : '';
$lang = isset($_POST['lang']) && strlen($_POST['lang']) > 0 ? $_POST['lang'] : '';
$category = isset($_POST['category']) && strlen($_POST['category']) > 0 ? $_POST['category'] : '';
$poem = isset($_POST['poem']) && strlen($_POST['poem']) > 0 ? $_POST['poem'] : '';


// Validate data.
if (isset($_POST['form_sent']) && $_POST['form_sent'] == "yes") {
	if (strlen($title) == 0) {
		$error_msg .= "<p> Introdu titlu. </p>";
	}
	if (strlen($category) == 0) {
		$error_msg .= "<p> Alege o categorie. </p>";
	}
	if (strlen($poem) == 0) {
		$error_msg .= "<p> Introdu poezia. </p>";
	}
	// Edit account information.
	if (strlen($error_msg) == 0) {
        Hop_editPoem($poemId, $title, $category, $poem);
	}
} else {
    // Get poem info.
    $q = mysql_query("SELECT * FROM " . TABLE_POEMS . " WHERE id = $poemId") or die(mysql_error());
    $r = mysql_fetch_assoc($q);
    if ($r) {
        $title = $r['title'];
        $lang = $r['lang'];
        $category = $r['category'];
        if ($lang == "english") {
            $english_category = $category;
        } else {
            $hindi_category = $category;
        }
        $poem = $r['poem'];
        $_POST['author'] = $r['author'];
        $_POST['lang'] = $r['lang'];
    }
}

// Smarty.
$smarty->assign('form_sent', $_POST['form_sent']);
$smarty->assign('error_msg', $error_msg);
$smarty->assign('title', $title);
$smarty->assign('lang', $lang);
$smarty->assign('category', $category);
$smarty->assign('poem', $poem);
$smarty->assign('categories', getCategories($category));
$smarty->assign('poemId', $poemId); 
$smarty->assign('authors', getAllMembers());

// Go!
$smarty->display('edit-poem.tpl');

// End page.
require('inc/end.php');
?>
